package org.lzuploader.gui.listeners.recap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.lzuploader.gui.WRecap;
import org.lzuploader.model.Localization;
import org.lzuploader.model.result.ResultCharacter;

/**
 * This class is used when user clicks on the character's name list.
 * @author Emmanuel Cron
 *
 */
public class ListSelectionListener implements SelectionListener
{
    /** The logger for this class. */
    private static Log log = LogFactory.getLog(ListSelectionListener.class);
    /** Localization of this class. */
    private static Localization localization = Localization.getInstance();
    
    /** Used when the widget is selected by default.
     * @param e The associated event
     */
    @Override
    public void widgetDefaultSelected(final SelectionEvent e)
    {
        widgetSelected(e);

    }

    /** Used when the user clicked on the widget.
     * @param e The associated event
     */
    @Override
    public void widgetSelected(final SelectionEvent e)
    {
        log.debug("User selected character in list.");
        
        log.trace("Fetching data.");
        
        Shell wRecapShell = null;
        for (int i = 0 ; i < e.display.getShells().length ; i++)
        {
            if (e.display.getShells()[i].getData() instanceof WRecap)
            {
                wRecapShell = e.display.getShells()[i];
                break;
            }
        }
        
        final WRecap recap = (WRecap) wRecapShell.getData();
        updateCharacterInfo(recap);
    }

    /**
     * Updates the character information displayed on the window, based on the current characters list selection.
     * @param recap The window that need to be updated.
     */
    public static void updateCharacterInfo(final WRecap recap)
    {
        final List cList = recap.getCharactersList();
        final String cSelected = cList.getItem(cList.getSelectionIndex());
        
        log.trace("Character selected: " + cSelected + ". Getting ResultCharacter.");
        
        ResultCharacter currentResultCharacter = null;
        
        for (ResultCharacter rs : recap.getResult().getImportedCharacters())
        {
            if (rs.getName().equals(cSelected))
            {
                currentResultCharacter = rs;
                break;
            }
        }
        
        log.trace("Current ResultCharacter: " + currentResultCharacter);
        
        log.trace("Updating controls.");
        
        switch (currentResultCharacter.getStatusCode())
        {
            case ResultCharacter.INVALID_GUILD:
                recap.getCharacterUpdateStatusLabel().setText(localization.getString("LABEL_INVALID_GUILD"));
                break;
            case ResultCharacter.NOT_OWNED:
                recap.getCharacterUpdateStatusLabel().setText(localization.getString("LABEL_NOT_OWNED"));
                break;
            case ResultCharacter.OK:
                recap.getCharacterUpdateStatusLabel().setText(localization.getString("LABEL_OK"));
                break;
            default:
                recap.getCharacterUpdateStatusLabel().setText(localization.getString("LABEL_UNKNOWN"));
        }
        
        // Professions
        if (currentResultCharacter.getProfessionsDeleted())
        {
            recap.getProfessionsDeletedStatusLabel().setText(localization.getString("LABEL_DELETED"));
        }
        else
        {
            recap.getProfessionsDeletedStatusLabel().setText(localization.getString("LABEL_FAILED"));
        }
        
        // Crafts
        if (currentResultCharacter.getCraftsUpdated() >= 0)
        {
            recap.getCraftsUpdatedStatusLabel().setText(String.valueOf(currentResultCharacter.getCraftsUpdated()));
        }
        else
        {
            recap.getCraftsUpdatedStatusLabel().setText(localization.getString("LABEL_FAILED"));
        }
        
        // Reagents
        if (currentResultCharacter.getReagentsUpdated() >= 0)
        {
            recap.getReagentsUpdatedStatusLabel().setText(String.valueOf(currentResultCharacter.getReagentsUpdated()));
        }
        else
        {
            recap.getReagentsUpdatedStatusLabel().setText(localization.getString("LABEL_FAILED"));
        }
        
        // Dungeons
        if (currentResultCharacter.getDungeonsUpdated() >= 0)
        {
            recap.getDungeonsUpdatedStatusLabel().setText(String.valueOf(currentResultCharacter.getDungeonsUpdated()));
        }
        else
        {
            recap.getDungeonsUpdatedStatusLabel().setText(localization.getString("LABEL_FAILED"));
        }
        
        // Crafts specialization
        if (currentResultCharacter.getCraftsSpecializationsUpdated() >= 0)
        {
            recap.getCraftsSpecializationsUpdatedStatusLabel().setText(String.valueOf(currentResultCharacter.getCraftsSpecializationsUpdated()));
        }
        else
        {
            recap.getCraftsSpecializationsUpdatedStatusLabel().setText(localization.getString("LABEL_FAILED"));
        }
    }
}
